<?
include_once("/var/www/html/new/com.inc.php");
include_once("/var/www/html/new/d/class/Member.php");
$txt=file_get_contents('php://input');
logfile($txt,'qunar_coop_log.txt');

$outSide=new SoapClient("http://open.12301.cc/openService/MXSE.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
$ac='123492';
$pw='b4b55c7adcff5bdbc3d5a969e85cb8b2';

$ac1='16ucom';
$pw1='c33367701511b4f6020ec61ded352059';
$inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac1|pw_16u=$pw1|auth_16u=true"));
$nowdate=date("Y-m-d H:i:s");

$arr_supplier=array(
	'FUJIANXIANXING'=>'RlVKSUFOWElBTlhJTkc=',
	'GULONGSHAN'=>'R1VMT05HU0hBTg',
	'MORUISHUISHIJIE'=>'TU9SVUlTSFVJU0hJSklF',
	'landiaoxyc'=>'bGFuZGlhb3h5Yw==',
	'YYTX'=>'WVlUWA==',
	'DEBUGSUPPLIER'=>'DEBUGSINGKEY'
);
//$txt = 'method=getProductByQunar&requestParam=%7B%22data%22%3A%22PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9InllcyI%2FPjxyZXF1ZXN0IHhtbG5zPSJodHRwOi8vcGlhby5xdW5hci5jb20vMjAxMy9RTWVucGlhb1JlcXVlc3RTY2hlbWEiPjxoZWFkZXI%2BPGFwcGxpY2F0aW9uPlF1bmFyLk1lbnBpYW8uQWdlbnQuQXV0b1Rlc3Q8L2FwcGxpY2F0aW9uPjxwcm9jZXNzb3I%2BU3VwcGxpZXJEYXRhRXhjaGFuZ2VQcm9jZXNzb3I8L3Byb2Nlc3Nvcj48dmVyc2lvbj52Mi4wLjA8L3ZlcnNpb24%2BPGJvZHlUeXBlPkdldFByb2R1Y3RCeVF1bmFyUmVxdWVzdEJvZHk8L2JvZHlUeXBlPjxjcmVhdGVVc2VyPlF1bmFyLk1lbnBpYW8uQWdlbnQuQXV0b1Rlc3Q8L2NyZWF0ZVVzZXI%2BPGNyZWF0ZVRpbWU%2BMjAxNC0wOS0xOSAxNDo1OTo0NTwvY3JlYXRlVGltZT48c3VwcGxpZXJJZGVudGl0eT5ERUJVR1NVUFBMSUVSPC9zdXBwbGllcklkZW50aXR5PjwvaGVhZGVyPjxib2R5IHhzaTp0eXBlPSJHZXRQcm9kdWN0QnlRdW5hclJlcXVlc3RCb2R5IiB4bWxuczp4c2k9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlIj48bWV0aG9kPkFMTDwvbWV0aG9kPjxjdXJyZW50UGFnZT4xPC9jdXJyZW50UGFnZT48cGFnZVNpemU%2BMTA8L3BhZ2VTaXplPjxyZXNvdXJjZUlkPgogICAgPC9yZXNvdXJjZUlkPjwvYm9keT48L3JlcXVlc3Q%2B%22%2C%22securityType%22%3A%22MD5%22%2C%22signed%22%3A%22B3B3360844A2D4E680D281914C4E1DB3%22%7D';
parse_str($txt,$arr);
if ($arr['method']=='getProductByQunar'){// 获取产品信息

    $rt=$arr['requestParam'];
	$arr1=json_decode($rt);
	$xml=base64_decode($arr1->data);
	$c=simpleXML_load_string($xml);
	$supplierIdentity=$c->header->supplierIdentity;
	$signkey=@$arr_supplier["$supplierIdentity"];		
	$method = $c->body->method; //方法
	$currentPage =$c->body->currentPage;// 当前页数
	$pageSize = $c->body->pageSize; //每页记录数
	$count = 0;	
	if($method == 'SINGLE'){//获取单个产品信息
	    $count = 1;
		$data_array = array();
		$data_array = get_date($c->body->resourceId,$inSide,$outSide,$ac,$pw,$count);

		$string = string_XML($data_array);
		
		$js_sing= singed($string,$signkey);
		
        echo $js_sing;		
	}
	elseif($method == 'ALL'){//获取全部产品信息
		$currentPage =$c->body->currentPage;// 当前页数
		$pageSize = $c->body->pageSize; //每页记录数
		$search = "select tid_aid from uu_qunar_use where supplierIdentity='$supplierIdentity'";
		$GLOBALS['le']->query($search);	
		$data = array();
		while($row=$GLOBALS['le']->fetch_assoc()){ 
			$data[] = $row;
		}
		foreach($data as $row){
			$count += 1;
			 //echo "--------".$i++."----------";
			$data_array = array();
			$data_array = get_date($row['tid_aid'],$inSide,$outSide,$ac,$pw,$count);	
			$string .= string_XML($data_array);		
			$js_sing= singed($string,$signkey);
		}
		//print_r($string);
		echo $js_sing;
	}
}
function singed($string,$signkey){//这里是计算加密
		$bstr=base64_encode($string);
		$signed=strtoupper(md5($signkey.$bstr));
		$arr=array('data'=>$bstr,'signed'=>$signed,'securityType'=>'MD5');		
		return json_encode($arr);
}
function get_date($tids,$inSide,$outSide,$ac,$pw,$count){//这里是获取数据
		$a_tids=explode('_',$tids);
		$tid=$a_tids[0]; //门票id
		$aid = $a_tids[1]; //供应商id
		
		$str="select a.id as lid,a.title as c,b.title as d ,a.address,b.pay,b.delaydays,b.tprice,b.pid,b.getaddr from uu_land a inner join uu_jq_ticket b on a.id = b.landid where b.id='$tid' limit 1";	
	    $GLOBALS['le']->query($str);
		$GLOBALS['le']->fetch_assoc();

		$resourceId = $tids; //去哪儿供应商产品id
		$sightName = $GLOBALS['le']->f('c'); //景区名称
		$city = $GLOBALS['le']->f('address'); //目的地
		$productName = $GLOBALS['le']->f('d'); //产品名称
		$pay = $GLOBALS['le']->f('pay'); //支付方式
		
		if($pay == 1){ //支付方式 PREPAY在线支付 CASHPAY景区支付
			$paymentType = 'PREPAY';
		}elseif($pay == 0){ 
			$paymentType ='CASHPAY'; 
		}
		
		$delaydays = $GLOBALS['le']->f('delaydays'); //允许推迟游玩的天数
		
		if($delaydays == 0){
			$validType = 'USE_DATE';	// 当天有效
			$searchday = 7;
		}else {
			$validType ='BETWEEN_USE_DATE_AND_N_DAYSAFTER'; //多少天内有效
			$searchday =$delaydays;
		} 
		
		if($validType ='BETWEEN_USE_DATE_AND_N_DAYSAFTER'){
			$daysAfterUseDaeValid = $delaydays;
		}elseif($validType ='USE_DATE'){
			$daysAfterUseDaeValid = 1; //游客选定的游玩日期起x天内有效
		}

		$pid = $GLOBALS['le']->f('pid'); //产品id
		$i_startime = time(date('Y-m-d'));
		$i_endtime = $i_startime + 86400*$searchday;
		
		$calendarPrices = '';
		for($i=$i_startime;$i<=$i_endtime;$i+= 86400){   
			$itime = date('Y-m-d',$i);
			$yz_order = $inSide->get_Dynamic_Price_Merge($pid,$itime,1,"","",1,"");
			if($yz_order == -1) continue;
			$get_gy_price = $outSide->__soapCall("Dynamic_Price_And_Storage",array("ac"=>$ac,"pw"=>$pw,"in0"=>$pid,"in1"=>$itime,"in2"=>1,"in3"=>0,"in4"=>1,"in5"=>$aid)); //查询供应价
			$get_gy_price_xml =simpleXML_load_string($get_gy_price); 
			$sellPrice = $get_gy_price_xml->Rec->UUsprice; //qunar供应价	

			// 9999 表示无限 
			$sellstock = ($get_gy_price_xml->Rec->UUstorage == -1) ? '9999':$get_gy_price_xml->Rec->UUstorage;
			
			$calendarPricesss=<<<XML
<calendarPrice>
<useDate>$itime</useDate>
<marketPrice>$yz_order</marketPrice>   
<sellPrice>$sellPrice</sellPrice>
<sellstock>$sellstock</sellstock>
<minimum></minimum>
<maximum></maximum>
</calendarPrice>
XML;
			$calendarPrices .= $calendarPricesss;	 
		}
		
		$remind =$GLOBALS['le']->f('getaddr'); //使用说明
		$smsTemplet = '您将会在3-5分钟内收到电子票，在景区售票处取票入园。';//短信模板
		$eticketType = 'S_STRING'; //电子票类型：短信
		$data_array= array( //需要的参数
			'count'=>$count,
			'resourceId'=>$tids,
			'sightName'=>$sightName,
			'city'=>$city,
			'productName'=>$productName,
			'paymentType'=>$paymentType,
			'validType'=>$validType,
			'daysAfterUseDateValid'=>$daysAfterUseDateValid,
			'useDate'=>$useDate,
			'marketPrice'=>$marketPrice,
			'sellPrice'=>$sellPrice,
			'remind'=>$remind,
			'smsTemplet'=>$smsTemplet,
			'eticketType'=>$eticketType,
			'calendarPrices'=>$calendarPrices
		);
		return $data_array;
}

function string_XML($string_array,$calendarPrices=null){ //xml的body部分
	$string=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<response xsi:schemaLocation="http://piao.qunar.com/2013/QMenpiaoResponseSchema QMResponseDataSchema-2.0.1.xsd" xmlns="http://piao.qunar.com/2013/QMenpiaoResponseSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
	<application>Qunar.Menpiao.Agent</application>
	<processor>SupplierDataExchangeProcessor</processor>
	<version>v2.0.1</version>
	<bodyType>GetProductByQunarResponseBody</bodyType>
	<createUser>SupplierSystemName</createUser>
	<createTime>yyyy-MM-dd HH:mm:ss</createTime>
	<code>1000</code>
	<describe>成功</describe>
</header>
<body xsi:type="GetProductByQunarResponseBody">
	<count>{$string_array['count']}</count>   
	<productInfos>
			<productInfo>
				<baseInfo>
					<resourceId>{$string_array['resourceId']}</resourceId>  
					<sights>
						<sight>
							<sightName>{$string_array['sightName']}</sightName> 
							<sightAddress></sightAddress>
							<city>{$string_array['city']}</city>
							<sightlon></sightlon>
							<sightlat></sightlat>
						</sight>
					</sights>
					<productName>{$string_array['productName']}</productName>   
				</baseInfo>
				<bookConfig>
					<paymentType>{$string_array['paymentType']}</paymentType>
					<advanceOption>
						<bookAdvanceDay></bookAdvanceDay>
						<bookAdvanceTime></bookAdvanceTime>
						<useAdvanceHour></useAdvanceHour>
					</advanceOption>
					<autoCancelTime></autoCancelTime>
					<contactPersonRequired>
						<nameRequired></nameRequired>
						<namePinyinRequired></namePinyinRequired>
						<emailRequired></emailRequired>
						<addressRequired></addressRequired>
					</contactPersonRequired>
					<bookPersonType></bookPersonType>
					<visitPersonRequiredForQuantity></visitPersonRequiredForQuantity>
					<visitPersonRequired>
						<nameRequired></nameRequired>
						<namePinYinRequired></namePinYinRequired>
						<credentialsRequired></credentialsRequired>
						<credentialsRequiredTypes></credentialsRequiredTypes>
						<userRequiredDefined1></userRequiredDefined1>
						<userRequiredDefined2></userRequiredDefined2>
					</visitPersonRequired>
					<perPhoneMaximum></perPhoneMaximum>
					<perPhoneMaximumType></perPhoneMaximumType>
				</bookConfig>
				<priceConfig>
					<validType>{$string_array['validType']}</validType>
					<daysAfterUseDateValid>{$string_array['daysAfterUseDaeValid']}</daysAfterUseDateValid>
					<daysAfterBookDateValid></daysAfterBookDateValid>
					<dayUseTimePeriod></dayUseTimePeriod>						
					<calendarPrices>{$string_array['calendarPrices']}
						
					</calendarPrices>
					<periodPrice>
						<periodStart></periodStart>
						<periodEnd></periodEnd>
						<validWeek></validWeek>
						<invalidDate></invalidDate>
						<marketPrice></marketPrice>
						<sellPrice></sellPrice>
						<sellstock></sellstock>
						<minimum></minimum>
						<maximum></maximum>
					</periodPrice>
				</priceConfig>
				<promoteConfig>
					<cashBackMoney></cashBackMoney>
				</promoteConfig>
				<productDescription>
					<feeInfo></feeInfo>
					<feeInclude></feeInclude>
					<feeExclude></feeExclude>
					<pickupTime></pickupTime>
					<pickupAddress></pickupAddress>
					<remind>{$string_array['remind']}</remind>
					<refundOption>
						<canRefund></canRefund>
						<refundApplyTimeBeforeValidEndDay></refundApplyTimeBeforeValidEndDay>
						<refundChargeType></refundChargeType>
						<refundCharge></refundCharge>
						<refundInfo></refundInfo>
					</refundOption>
				</productDescription>
				<otherConfig>
					<telephone></telephone>
					<salePeriods>
						<salePeriod>
							<saleStartTime></saleStartTime>
							<saleEndTime></saleEndTime>
						</salePeriod>
					</salePeriods>
					<smsTemplet>{$string_array['smsTemplet']}</smsTemplet>
					<eticketType>{$string_array['eticketType']}</eticketType>
				</otherConfig>
			</productInfo>
	</productInfos>
</body>
</response>
XML;
	return $string;
}
function logfile($txt,$path){
	$fp = fopen($path,"a");
	flock($fp, LOCK_EX) ;
	fwrite($fp,$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp); 
}
function Err_Code($errcode){
	$arr_X_code=array(101=>20027,105=>12001,107=>90005,112=>20006,115=>90007,116=>14007,119=>13009,123=>13001,130=>20005,131=>20006,133=>14009,134=>14010,1061=>20004,1062=>20022,1065=>13007,1071=>20010,1072=>20007,1075=>13004,1077=>13005);
	$ecode=@$arr_X_code[$errcode];
	if (!$ecode) $ecode=90001;
	return $ecode;
}
function Err_Word($errcode){
	$arr_X_word=array(
		103=>'您选择的出行日期格式不合法',
		101=>'创建订单异常，用户未登陆',
		105=>'产品不存在，不可预订',
		107=>'供应商交互XML解析错误',
		112=>'创建订单异常，取票人手机号码异常',
		115=>'web服务调用错误',
		116=>'电子票最多重复发送5次',
		119=>'订单状态不正确，不能重新发送入园凭证',
		123=>'订单不存在',
		130=>'创建订单异常，取票人邮箱格式不合法',
		131=>'创建订单异常，取票人手机号码异常',
		133=>'订单消费失败，订单还未到有效使用日期',
		134=>'订单消费失败，订单已超过有效使用日期',
		1061=>'创建订单异常，选购产品不存在',
		1062=>'创建订单异常，库存不足',
		1065=>'订单产品价格信息异常',
		1071=>'创建订单异常，出行日期为空',
		1072=>'创建订单异常，取票人姓名异常',
		1075=>'订单存在，但不属于该用户',
		1077=>'该订单无法修改'
	);
	$eword=@$arr_X_word[$errcode];
	if (!$eword) $eword='其他错误';
	return $eword;
}
?>